import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import redis.clients.jedis.Jedis;

public class Redis {

	public void test() {
		Jedis redis = new Jedis("127.0.0.1", 6379);// 连接redis
		// redis.auth("redis");// 验证密码,如果需要验证的话
		// STRING 操作
		// SET key value将字符串值value关联到key。
		redis.set("name", "wangjun1");
		redis.set("id", "123456");
		redis.set("address", "guangzhou");
		// SETEX key seconds value将值value关联到key，并将key的生存时间设为seconds(以秒为单位)。
		redis.setex("foo", 5, "haha");
		// MSET key value [key value ...]同时设置一个或多个key-value对。
		redis.mset("haha", "111", "xixi", "222");
		// redis.flushAll();清空所有的key
		System.out.println(redis.dbSize());// dbSize是多少个key的个数
		// APPEND key value如果key已经存在并且是一个字符串，APPEND命令将value追加到key原来的值之后。
		redis.append("foo", "00");// 如果key已经存在并且是一个字符串，APPEND命令将value追加到key原来的值之后。
		// GET key 返回key所关联的字符串值
		redis.get("foo");
		// MGET key [key ...] 返回所有(一个或多个)给定key的值
		List list = redis.mget("haha", "xixi");
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}

		redis.rpush("mylist", "aaa");
		redis.hset("myhash", "field", "value");

	}
	/***
	 * 序列化和反序列化的调用
	 */
	public static void SerializeUtil() {
		Jedis jedis = new Jedis("127.0.0.1", 6379);// 连接redis
		 People p = new People(28, "李强", 1); // peson类记得实现序列化接口 Serializable
		 p.setAge(20);
		 p.setName("姚波");
		 p.setId(1);
		 jedis.set("people".getBytes(), serialize(p));	//序列化并写入redis
		 byte[] byt = jedis.get("people".getBytes());	//从redis取出
		 Object obj = unserizlize(byt);
		 if (obj instanceof People) {
//		 System.out.println(((People) obj).getAge());
		 }
	}

	/***
	 * 序列化
	 * @param object 对象
	 * @return 字节流
	 */
	public static byte[] serialize(Object object) {
		ObjectOutputStream obi = null;
		ByteArrayOutputStream bai = null;
		try {
			bai = new ByteArrayOutputStream();
			obi = new ObjectOutputStream(bai);
			obi.writeObject(object);
			byte[] byt = bai.toByteArray();
			return byt;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static byte[] aa(Object object){
		byte[] jj = null ;
		ObjectOutputStream oos = null;
		ByteArrayOutputStream bos = null;
		try {
			bos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(bos);
			oos.writeObject(object);
			jj = bos.toByteArray();
			
			return jj;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Object bb(byte[] jj){
		Object object = null;
		ObjectInputStream ois = null;
		ByteArrayInputStream bis = null;
		try {
			bis = new ByteArrayInputStream(jj);
			ois = new ObjectInputStream(bis);
			object = ois.readObject();
			
			return object;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/***
	 * 反序列化
	 * @param byt 字节流
	 * @return Object类型
	 */
	public static Object unserizlize(byte[] byt) {
		ObjectInputStream oii = null;
		ByteArrayInputStream bis = null;
		try {
			bis = new ByteArrayInputStream(byt);
			oii = new ObjectInputStream(bis);
			Object obj = oii.readObject();
			return obj;
		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;
	}

	
	public static byte[] cc(Object object){
		byte[] jj = null;
		ObjectOutputStream oos = null;
		ByteArrayOutputStream bos = null;
		try {
			bos = new ByteArrayOutputStream();
			oos = new ObjectOutputStream(bos);
			oos.writeObject(object);
			jj = bos.toByteArray();
			return jj;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Object dd(byte[] jj){
		Object object = null;
		ObjectInputStream ois = null;
		ByteArrayInputStream bis = null;
		try {
			bis = new ByteArrayInputStream(jj);
			ois = new ObjectInputStream(bis);
			object = ois.readObject();
			return object;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static  void ccdd(){
		File file = new File("c://1.txt");
		byte[] data = new byte[(int) file.length()];
		Jedis jedis = new Jedis("127.0.0.1", 6379);
		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			fileInputStream.read(data);
			jedis.set("file".getBytes(), data);
			
			byte[] asd = jedis.get("file".getBytes());
			String ss = new String(asd,"utf-8");
			System.out.println(ss);
			fileInputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public static  void ccdd(int i){
		File file = new File("c://1.txt");
		byte[] data = new byte[(int) file.length()];
		Jedis jedis = new Jedis("127.0.0.1", 6379);
		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			fileInputStream.read(data);
			jedis.set("file".getBytes(), data);
			jedis.hset("file2".getBytes(), (i + "").getBytes(), data);
			
			byte[] asd = jedis.get("file".getBytes());
			String ss = new String(asd,"utf-8");
			System.out.println(ss);
			fileInputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	public static  void jedisSetHash(){
		Jedis jedis = new Jedis("127.0.0.1", 6379);
		Map<String, People> map = new HashMap<String, People>();
		People p = null;
		for(int i = 0; i < 10; i++){
			p = new People(i+20, "李强-" + i, i);
			map.put(p.id + "", p);
		}
		
		
		
	}
	
	public static void main(String[] args) {
		// Redis t1 = new Redis();
		// t1.test();
//		SerializeUtil();
//		for(int i = 0; i < 10; i++)
//			ccdd(i);
		ccdd();
	}
}
